﻿var keyValue = request('keyValue');

var bootstrap = function ($, learun) {
    "use strict";

    var page = {
        init: function () {
            page.bind();
            page.initData();
        },
        bind: function () {
            var userlist = [];
            var userlist1 = [];
            var userlist2 = [];
            learun.clientdata.getAllAsync('department', {
                callback: function (departmentData) {
                    learun.clientdata.getAllAsync('user', {
                        callback: function (data) {
                            $.each(data, function (_id, _item) {
                                var _text = _item.name;
                                departmentData[_item.departmentId] && (_text = departmentData[_item.departmentId].name + ":" + _text);
                                if (_id != 'System') {
                                    var point = {
                                        id: _id,
                                        text: _text
                                    };
                                    userlist.push(point);
                                    userlist1.push(point);
                                    userlist2.push(point);
                                }
                            });

                            $('#address').lrselect({
                                type: 'multiple',
                                data: userlist,
                                allowSearch: true
                            });
                            $('#copysend').lrselect({
                                type: 'multiple',
                                data: userlist1,
                                allowSearch: true
                            });
                            $('#bccsend').lrselect({
                                type: 'multiple',
                                data: userlist2,
                                allowSearch: true
                            });
                        }
                    });
                }
            });
            //内容编辑器
            var ue = UE.getEditor('content');
            if (!!keyValue) {
                var selecedRow = config.getParent(learun).selectedRow;
                ue.ready(function(){
                    ue.setContent(selecedRow.content);
                })
            }
            $('#content')[0].ue = ue;

            $('#files').lrUploader();

            // 保存草稿
            $('#btn_draft').on('click', function () {
                if (!$('#form').lrValidform()) {
                    return false;
                }
                var postData = {};
                var formData = $('#form').lrGetFormData(keyValue);
                var recipientList = [];
                var addressIds = formData.address.split(",");
                for (var i = 0; i < addressIds.length; i++) {
                    recipientList.push({"recipientId": addressIds[i], "recipientStatus": 0})
                }
                var copysendIds = formData.copysend.split(",");
                for (var i = 0; i < copysendIds.length; i++) {
                    recipientList.push({"recipientId": copysendIds[i], "recipientStatus": 1})
                }
                var bccsendIds = formData.bccsend.split(",");
                for (var i = 0; i < bccsendIds.length; i++) {
                    recipientList.push({"recipientId": bccsendIds[i], "recipientStatus": 2})
                }

                postData = {
                    keyValue: keyValue,
                    theme: formData.theme,
                    files: formData.files,
                    content: formData.content,
                    recipientList: recipientList,
                    sendStatus: 0,
                    status: 0
                };
                $.lrSaveForm(config.base_server() + 'emails/saveOrUpdate?access_token=' + config.getToken().access_token + '&keyValue=' + keyValue, JSON.stringify(postData), function (res) {
                    // 保存成功后才回调
                    config.getParent(learun).refreshGirdData();
                });
            });
            // 发送邮件
            $('#btn_send').on('click', function () {
                if (!$('#form').lrValidform()) {
                    return false;
                }
                var postData = {};
                var formData = $('#form').lrGetFormData(keyValue);
                var recipientList = [];
                var addressIds = formData.address.split(",");
                for (var i = 0; i < addressIds.length; i++) {
                    if(addressIds[i]!='') {
                        recipientList.push({"recipientId": addressIds[i], "recipientStatus": 0})
                    }
                }
                var copysendIds = formData.copysend.split(",");
                for (var i = 0; i < copysendIds.length; i++) {
                    if(copysendIds[i]!='') {
                        recipientList.push({"recipientId": copysendIds[i], "recipientStatus": 1})
                    }
                }
                var bccsendIds = formData.bccsend.split(",");
                for (var i = 0; i < bccsendIds.length; i++) {
                    if(bccsendIds[i]!='') {
                        recipientList.push({"recipientId": bccsendIds[i], "recipientStatus": 2})
                    }
                }
                postData = {
                    keyValue: keyValue,
                    theme: formData.theme,
                    files: formData.files,
                    content: formData.content,
                    recipientList: recipientList,
                    sendStatus: 1,
                    status: 1
                };
                $.lrSaveForm(config.base_server() + 'emails/saveOrUpdate?access_token=' + config.getToken().access_token + '&keyValue=' + keyValue, JSON.stringify(postData), function (res) {
                    // 保存成功后才回调
                    config.getParent(learun).refreshGirdData();
                });
            });
        },
        initData: function () {
            if (!!keyValue) {
                var selecedRow = config.getParent(learun).selectedRow;
                var formData = {
                    theme: selecedRow.theme,
                    files: selecedRow.files,
                    content: selecedRow.content,
                    address: selecedRow.address,
                    copysend: selecedRow.copysend,
                    bccsend: selecedRow.bccsend
                }
                console.log(formData);
                $('#form').lrSetFormData(formData);
                ue.setContent(selecedRow.content);
            }
        }
    };
    page.init();
}